HW10: Graphics of Storm Tracks in the North Atlantic

Week 10: Iterations and Loops

Author

STAT 33B, Fall 2025.

General Instructions
  • Use the template file hw10-template.qmd to write your answers (e.g. code, descriptions).

  • Rename this file as hw10-first-last.qmd, where first and last are your first and last names (e.g. hw10-gaston-sanchez.qmd).

  • Make sure the YAML header in your qmd files includes embed-resources: true

  • Submit both your qmd and HTML files to the corresponding assignment submission in bCourses.

  • Please note that if you only submit one file (either HTML or qmd), a 10% deduction will be applied.

  • Please note that if you submit the incorrect files you will very likely receive no credit.

  • If you use AI tools to answer any part, please indicate that you use such a tool, and also include the prompt(s) that you used to obtain an answer.

1 Animated Visualization with GIF files

In this assignment you will create a couple of GIF files to visualize storm tracks in the North Atlantic. This is related to what you did in HW6 but this time you won’t use "gganimate", instead you will produce GIF images with some functions from the package "magick".

# packages
library(tidyverse)  # ecosystem of data science packages
library(spData)     # data set for spatial analysis
library(magick)     # for gif animation
storms
# A tibble: 19,537 × 13
   name   year month   day  hour   lat  long status      category  wind pressure
   <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <fct>          <dbl> <int>    <int>
 1 Amy    1975     6    27     0  27.5 -79   tropical d…       NA    25     1013
 2 Amy    1975     6    27     6  28.5 -79   tropical d…       NA    25     1013
 3 Amy    1975     6    27    12  29.5 -79   tropical d…       NA    25     1013
 4 Amy    1975     6    27    18  30.5 -79   tropical d…       NA    25     1013
 5 Amy    1975     6    28     0  31.5 -78.8 tropical d…       NA    25     1012
 6 Amy    1975     6    28     6  32.4 -78.7 tropical d…       NA    25     1012
 7 Amy    1975     6    28    12  33.3 -78   tropical d…       NA    25     1011
 8 Amy    1975     6    28    18  34   -77   tropical d…       NA    30     1006
 9 Amy    1975     6    29     0  34.4 -75.8 tropical s…       NA    35     1004
10 Amy    1975     6    29     6  34   -74.8 tropical s…       NA    40     1002
# ℹ 19,527 more rows
# ℹ 2 more variables: tropicalstorm_force_diameter <int>,
#   hurricane_force_diameter <int>
Plotting storm tracks reminder

Please refer to HW6 to review how to graph storm tracks of tropical cyclones in the North Atlantic.

Recall that you need the following tables:

  • data set: storms (from "tidyverse")
  • map data: world (from "spData")


2 Your Turn: Making Individual Graphics for Years 2000-2022

  • Your goal is to make and export individual graphics of storm tracks for every year between 2000 and 2022.

  • Graph maps of storm tracks making sure that the visual appearance of each graphic is okay. In other words, the quality of the graphics should be okay to share with a general audience, with your boss, clients, on social media, etc.

  • Write a for loop to export the images as PNG files.

  • Export each PNG file using ggsave(), specifying width = 6 and height = 4 in inches.

  • Hint: the function paste0() can help you in the creation of the file names: e.g. “storm_tracks_2000.png”, “storm_tracks_2001.png”, …, “storm_tracks_2022.png”

for (yr in 2000:2022) {
  ggplot(data = filter(storms, year == yr),
       mapping = aes(x = lat, y = long, group = name, color = name)) + geom_point(size = 1, alpha = 0.3) + labs(title = yr)
  ggsave(paste0("storm_tracks_", yr, ".png"), width = 6, height = 4)
}


3 Your Turn: Creating a GIF from exported PNGs

Once you have the set of PNG files (one PNG per year), the next stage involves importing them in R to create the GIF image. Follow the steps listed below to accomplish this task:

  1. Create a character vector pngs with the names of the PNG files.

    • BTW: Make sure the PNG files are in the same directory of your qmd file!!!
  2. Run the command png_list <- lapply(pngs, image_read) to import all the PNG files into a list.

  3. Apply the image_join() function to the list png_list to combine all the PNG files into a single image. Call the output object img_joined, which is a magick image object.

  4. Apply the function image_animate() to your magick image object img_joined. This will create the animation. Call the output object img_animated. You may want to test different values for the arguments fps and delay, e.g fps = 2, delay = 100, depending on how fast or slow you want the GIF to be played.

  5. Finally, export your object img_animated to a GIF file with the function image_write(). You can call the exported file storm-tracks.gif or something like that.

pngs <- c()
for (yr in 2000:2022) {
  pngs <- c(pngs, paste0("storm_tracks_", yr, ".png"))
}
png_list <- lapply(pngs, image_read)
img_joined <- image_join(png_list)
img_animated <- image_animate(img_joined, fps = 2, delay = 100)
image_write(img_animated, "storm-tracks.gif")
img_animated


4 Your Turn: Rinse and Repeat

Make another GIF animation to visualize storm tracks of your choice, based on at least 10 individual graphics.

For instance, you can focus on tropical cyclones in certain months, or also focus on certain kind of storms (hurricanes in general, or of a certain category).

Use this opportunity to practice your tidyverse skills for manipulating data, and making beautiful graphics.

Important: For the creation of the PNG files, do this with a while loop instead of a for-loop.

yr <- 1975
while (yr <= 2020) {
  ggplot(data = filter(storms, year == yr & status == "tropical storm"),
       mapping = aes(x = lat, y = long, group = name, color = name)) + geom_point(size = 1, alpha = 0.3) + labs(title = yr)
  ggsave(paste0("tropicals", yr, ".png"), width = 6, height = 4)
  yr <- yr + 1
}

pngs1 <- c()
for (yr in 1975:2020) {
  pngs1 <- c(pngs1, paste0("tropicals", yr, ".png"))
}
png_list1 <- lapply(pngs1, image_read)
img_joined1 <- image_join(png_list1)
img_animated1 <- image_animate(img_joined1, fps = 2, delay = 100)
image_write(img_animated1, "tropicals.gif")
img_animated1